/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.controller.admin;

import com.lehe.common.utils.StringUtil;
import com.lehe.entry.*;
import com.lehe.entry.page.PageDto;
import com.lehe.service.impl.AdminMenuService;
import com.lehe.service.impl.AdminRoleService;
import com.lehe.service.impl.AdminUserService;
import com.lehe.util.AceTreeUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 类的功能描述
 *
 * @author 王丹
 * @since 2017-03-08
 */
@RequestMapping("admin/systemManager")
@Controller
public class SystemManagerController extends BaseAdminController {

    @Autowired
    AdminUserService adminUserService;
    @Autowired
    AdminRoleService adminRoleService;
    @Autowired
    AdminMenuService adminMenuService;

    @RequestMapping("userManager")
    public String userIndex(Model model){
        return "admin/systemManager/userManager";
    }

    @RequestMapping("userManagerList")
    public String userManagerList(SqlParameterDto sqlParameterDto, Model model){
        PageDto pd = new PageDto(sqlParameterDto.getPage(), sqlParameterDto.getPageSize());
        sqlParameterDto.setPage(pd.getPageIndex());
        sqlParameterDto.setPageSize(pd.getPageSize());
        Long count = adminUserService.queryAdminUserCountBymult(sqlParameterDto);
        List<AdminUserDto> list = adminUserService.queryAdminUserBymult(sqlParameterDto);
        model.addAttribute("data",list);
        pd.setRecordCount(count.intValue());
        model.addAttribute("page",pd);
        model.addAttribute("parameterDto",sqlParameterDto);
        return "admin/systemManager/userManagerList";
    }

    @RequestMapping("addUser")
    public String addUser(SqlParameterDto sqlParameterDto, Model model){
        /*if(null!=sqlParameterDto.getUserId()){
            model.addAttribute("userDto", adminUserService.queryById(sqlParameterDto.getUserId()));
        }*/
        model.addAttribute("roleList", adminRoleService.queryAll());
        return "admin/systemManager/userAdd";
    }

    @RequestMapping("insertUserInfo")
    @ResponseBody
    public BaseResponse insertUserInfo(AdminUserDto userDto){
        return adminUserService.insertAdminUser(userDto);
    }

    @RequestMapping("userSetting")
    public String userSetting(Model model){
        model.addAttribute("userDto", adminUserService.queryById(this.getAdminUserId()));
        return "admin/systemManager/userSetting";
    }

    @RequestMapping("modifyUserInfo")
    @ResponseBody
    public BaseResponse modifyUserInfo(Model model, String pwd, String newPwd, String repeatPwd){
        BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "保存失败");
        if(StringUtil.isBlank(pwd) || StringUtil.isBlank(newPwd) || StringUtil.isBlank(repeatPwd)){
            return baseResponse;
        }
        if(!newPwd.equals(repeatPwd)){
            baseResponse.setMsg("重复密码不一致");
            return baseResponse;
        }
        AdminUserDto adminUserDto = adminUserService.queryById(this.getAdminUserId());
        if(!DigestUtils.md5Hex(pwd).equals(adminUserDto.getPwd())){
            baseResponse.setMsg("旧密码不正确");
            return baseResponse;
        }
        adminUserDto = new AdminUserDto();
        adminUserDto.setId(this.getAdminUserId());
        adminUserDto.setPwd(DigestUtils.md5Hex(newPwd));
        adminUserService.updateAdminUserById(adminUserDto);
        baseResponse.setCode(BaseResponse.SUCCESS);
        baseResponse.setMsg("设置密码成功");
        return baseResponse;
    }

    @RequestMapping("delUser")
    @ResponseBody
    public BaseResponse delUser(Long id, String key){
        return adminUserService.delUserById(id);
    }

    @RequestMapping("roleManager")
    public String roleIndex(Model model){
        return "admin/systemManager/roleManager";
    }

    @RequestMapping("roleManagerList")
    public String roleManagerList(SqlParameterDto sqlParameterDto, Model model){
        PageDto pd = new PageDto(sqlParameterDto.getPage(), sqlParameterDto.getPageSize());
        sqlParameterDto.setPage(pd.getPageIndex());
        sqlParameterDto.setPageSize(pd.getPageSize());
        Long count = adminRoleService.queryAllCount(sqlParameterDto);
        List<AdminRoleDto> list = adminRoleService.queryAll(sqlParameterDto);
        model.addAttribute("data",list);
        pd.setRecordCount(count.intValue());
        model.addAttribute("page",pd);
        model.addAttribute("parameterDto",sqlParameterDto);
        return "admin/systemManager/roleManagerList";
    }

    @RequestMapping("addRole")
    public String addRole(SqlParameterDto sqlParameterDto, Model model){
        model.addAttribute("menus", AceTreeUtil.biuldTree(adminMenuService.queryAll(), null));
        return "admin/systemManager/roleAdd";
    }

    @RequestMapping("insertRoleInfo")
    @ResponseBody
    public BaseResponse insertRoleInfo(AdminRoleDto roleDto){
        return adminRoleService.insertAdminRole(roleDto);
    }

    @RequestMapping("modifyRole")
    public String modifyRole(SqlParameterDto sqlParameterDto, Model model){
        model.addAttribute("role", adminRoleService.queryById(sqlParameterDto.getId()));
        model.addAttribute("menus", AceTreeUtil.biuldTree(adminMenuService.queryAll(), adminMenuService.queryMenusByRoleId(sqlParameterDto.getId())));
        return "admin/systemManager/roleUpdate";
    }

    @RequestMapping("updateRoleInfo")
    @ResponseBody
    public BaseResponse updateRoleInfo(AdminRoleDto roleDto){
        return adminRoleService.updateAdminUserById(roleDto);
    }

    @RequestMapping("menuManager")
    public String menuIndex(Model model){
        return "admin/systemManager/menuManager";
    }

    @RequestMapping("menuManagerList")
    public String menuManagerList(SqlParameterDto sqlParameterDto, Model model){
        PageDto pd = new PageDto(sqlParameterDto.getPage(), sqlParameterDto.getPageSize());
        sqlParameterDto.setPage(pd.getPageIndex());
        sqlParameterDto.setPageSize(pd.getPageSize());
        Long count = adminMenuService.queryAllCount(sqlParameterDto);
        List<AdminMenuDto> list = adminMenuService.queryAll(sqlParameterDto);
        model.addAttribute("data",list);
        pd.setRecordCount(count.intValue());
        model.addAttribute("page",pd);
        model.addAttribute("parameterDto",sqlParameterDto);
        return "admin/systemManager/menuManagerList";
    }

    @RequestMapping("addMenu")
    public String addMenu(SqlParameterDto sqlParameterDto, Model model){
        model.addAttribute("menus", AceTreeUtil.biuldTree(adminMenuService.queryAll(), null));
        return "admin/systemManager/menuAdd";
    }

    @RequestMapping("insertMenuInfo")
    @ResponseBody
    public BaseResponse insertMenuInfo(AdminMenuDto menuDto){
        return adminMenuService.insertAdminRole(menuDto);
    }

    @RequestMapping("modifyMenu")
    public String modifyMenu(SqlParameterDto sqlParameterDto, Model model){
        String[] s = {sqlParameterDto.getId().toString()};
        model.addAttribute("menu", adminMenuService.queryMenusByIds(s).get(0));
        return "admin/systemManager/menuUpdate";
    }

    @RequestMapping("updateMenuInfo")
    @ResponseBody
    public BaseResponse updateMenuInfo(AdminMenuDto menuDto, String key){
        return adminMenuService.updateAdminUserById(menuDto);
    }

    @RequestMapping("delMenu")
    @ResponseBody
    public BaseResponse delMenu(Long id, String key){
        return adminMenuService.deleteById(id, key);
    }

}
